package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.workbench.domain.*;
import com.bjpowernode.crm.workbench.mapper.CustomerMapper;
import com.bjpowernode.crm.workbench.mapper.TranHistoryMapper;
import com.bjpowernode.crm.workbench.mapper.TranMapper;
import com.bjpowernode.crm.workbench.service.CustomerService;
import com.bjpowernode.crm.workbench.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TranServiceImpl implements TranService {

    @Autowired
    private TranMapper tranMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private TranHistoryMapper tranHistoryMapper;

    @Override
    public List<Tran> queryTranByConditionForPage(Map<String, Object> map) {
        return tranMapper.selectTranByConditionForPage(map);
    }

    @Override
    public int queryCountOfTranByConditionForPage(Map<String, Object> map) {
        return tranMapper.selectCountOfTranByConditionForPage(map);
    }

    @Override
    public boolean saveCreateTran(Map<String, Object> map) {
        boolean flag = true;
        String customerName = (String)map.get("customerName");
        User user = (User)map.get(Contants.SESSION_USER);

        try {
            Customer customer = customerMapper.selectCustomerByName(customerName);

            if(customer==null){
                //新建
                customer = new Customer();
                customer.setId(UUIDUtils.getUUID());
                customer.setName(customerName);
                customer.setOwner(user.getId());
                customer.setCreateBy(user.getId());
                customer.setCreateTime(DateUtils.formatDataTime(new Date()));
                customerMapper.insertCustomer(customer);
            }

            Tran tran = new Tran();

            tran.setStage((String)map.get("stage"));
            tran.setOwner((String)map.get("owner"));
            tran.setNextContactTime((String)map.get("nextContactTime"));
            tran.setName((String)map.get("name"));
            tran.setId(UUIDUtils.getUUID());
            tran.setExpectedDate((String)map.get("expectedDate"));
            tran.setCustomerId(customer.getId());
            tran.setCreateTime(DateUtils.formatDataTime(new Date()));
            tran.setCreateBy(user.getId());
            tran.setContactSummary((String)map.get("contactSummary"));
            tran.setContactsId((String)map.get("contactsId"));
            tran.setActivityId((String)map.get("activityId"));
            tran.setDescription((String)map.get("description"));
            tran.setSource((String)map.get("source"));
            tran.setType((String)map.get("type"));
            tran.setMoney((String)map.get("money"));

            tranMapper.insertTran(tran);

            TranHistory tranHistory = new TranHistory();
            tranHistory.setId(UUIDUtils.getUUID());
            tranHistory.setMoney(tran.getMoney());
            tranHistory.setTranId(tran.getId());
            tranHistory.setCreateBy(tran.getCreateBy());
            tranHistory.setCreateTime(tran.getCreateTime());
            tranHistory.setExpectedDate(tran.getExpectedDate());
            tranHistory.setStage(tran.getStage());

            tranHistoryMapper.insertTranHistory(tranHistory);
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }

        return flag;
    }

    @Override
    public Tran queryTranByIdForDetail(String id) {
        return tranMapper.selectTranByIdForDetail(id);
    }

    @Override
    public List<FunnelVO> queryCountOfTranGroupByStage() {
        return tranMapper.selectCountOfTranGroupByStage();
    }
}